import { getQuotationShipmentInfoApi } from "@/api/waybillManagement/warehouseShipment";
import { message } from "ant-design-vue";
import { reactive, ref } from "vue";

export default function InitHook(state: { emit: any }) {
  const { emit } = state;
  const formState = ref<any>({
    bizPreOrderShipmentResDto: {},
    bizOrderShipmentQuotation: {},
    bizOrderShipmentCosts: [],
    appendCost: {
      list: [],
      totalPrice: 0,
      type: undefined,
      typeName: ""
    },
    freightCost: {
      list: [],
      totalPrice: 0,
      type: undefined,
      typeName: ""
    },
    addCost: {
      list: [],
      totalPrice: 0,
      type: undefined,
      typeName: ""
    },
    sumTotalPrice: 0
  });
  const loading = ref<boolean>(false);
  const dialogOpts = reactive<Base.DialogOpts>({
    title: "修改送货仓库",
    width: "720px",
    type: 1,
    passData: {},
    component: "",
    visible: false
  });
  const drawerOpts = reactive<Base.DrawerOpts>({
    title: "修改送货仓库",
    width: "720px",
    type: 1,
    passData: {},
    component: "",
    visible: false
  });
  // 有报价ID
  const init = (id: string, selectNext?: boolean) => {
    loading.value = true;
    return getQuotationShipmentInfoApi({ id, selectNext: selectNext ? selectNext : false })
      .then((res: any) => {
        const { code, data } = res;
        if (code === 200) {
          if (data) {
            formState.value = data;
          } else {
            message.info("已经是最后一条数据!");
            emit("submit");
          }
        }
      })
      .finally(() => {
        loading.value = false;
      });
  };
  return {
    formState,
    loading,
    dialogOpts,
    drawerOpts,
    init
  };
}
